﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PowerCreator.LMS.DL.Project.Entity;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Web.DL.Project;
public partial class sysadmin_DL_Project_ManageSchool:PowerCreator.LMS.Web.Base.WebBasePage
{
    TOSchoolGroup opSG = new TOSchoolGroup();
    private PowerCreator.LMS.Web.DL.User.LoginEntity login = new PowerCreator.LMS.Web.DL.User.LoginEntity();
    private string url = "../../login.aspx";


    protected void Page_Load(object sender, EventArgs e)
    {
        CheckUser();
        if (!IsPostBack)
        {
            InitPage();

            BinderRpt();
        }
    }
    private void CheckUser()
    {
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {
            Script.RedirectNewUrl(url);
        }

        login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
        if (login == null)
        {
            Script.RedirectNewUrl(url);
        }

    }
    private int CheckPopedom()
    {
        int rt = 0;

        login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
        if (login != null)
        {
            if (login.VOAdvancedUser == null)
            {
                Script.RedirectNewUrl(url);
            }
            else 
                rt = login.VOAdvancedUser.AUserGroup;
        }
        return rt;

    }
    private string GetManagerRegionIDList(int userID)
    {
        string rt = "0";
        if (userID > 0)
        {
            string search = "ManagerID="+userID.ToString();
            DataTable dt = new DataTable();
            TORegion obj = new TORegion();
            dt = obj.Query(search);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                rt = rt + "," + dt.Rows[i]["Region"].ToString();
            }
        }
        rt = rt + ",0";
        return rt;
    } 

    protected override void InitPage()
    {
        lbl_InfoTitle.Text = GetLanageValueByKey("lbl_Department_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
       
        base.InitPage();
    }

    //绑定Rpt
    private void BinderRpt()
    {
        string search = "1=1 ";
        //如果是超级管理员登录可以查看所有课程
        if (GetLoginEntity.UserName.ToLower() != "admin" && GetLoginEntity.UserName.ToLower() != "administrator")
        {
            search = search + System.String.Format("and (SchoolGroup_Id in (Select FkOID from TauthorityUser as a , TauthorityList as b where a. FkAuthorityID = b.FkAuthorityID and FkUserID ={0} and b.authorityType=1) or CreatorId ={0})", GetLoginEntity.UserID);

        }
        DataTable dt = opSG.Query(search);
        //ArrayList list = new ArrayList();
        DataTable dt2 = new DataTable();
        dt2 = opSG.GetSchoolGroupTree(dt);
        if (dt2.Rows.Count == 0)
        {
            DataRow dr = dt2.NewRow();
            dr["Text"] = "默认学校";
            dr["Value"] ="0";
            dr["SchoolGroupRegion"] = "0";
            dr["SchoolGroupLevel"] = "03";
            dt2.Rows.Add(dr);

        }

        rptSchoolGroup.DataSource = dt2;
        rptSchoolGroup.DataBind();
        string MyReginIDList = "0,0";
        if (CheckPopedom() == 2)
        {
            MyReginIDList = GetManagerRegionIDList(login.UserID);
        } 
        for (int i = 0; i < rptSchoolGroup.Items.Count; i++)
        {
            Label lbl_level=(Label)rptSchoolGroup.Items[i].FindControl("lbl_level");
            Label lbl_region=(Label)rptSchoolGroup.Items[i].FindControl("lbl_region");
            Button btnadd = (Button)rptSchoolGroup.Items[i].FindControl("btn_Add");
            btnadd.Text = GetLanageValueByKey("btn_AddNew", PowerCreator.LMS.Language.LanagetItemType.Page);
            Button btn_Editor = (Button)rptSchoolGroup.Items[i].FindControl("btn_Editor");
            btn_Editor.Text = GetLanageValueByKey("ManagerRole_gv_lnk_o1", PowerCreator.LMS.Language.LanagetItemType.Page);
            Button btn_del = (Button)rptSchoolGroup.Items[i].FindControl("btn_del");
            btn_del.Text = GetLanageValueByKey("ManagerRole_gv_lnk_o2", PowerCreator.LMS.Language.LanagetItemType.Page);
            btn_del.Attributes.Add("onClick", "javascript:return confirm('确定删除此记录？');");

            if (CheckPopedom() > 2)
            {
                btn_del.Enabled = false;
                btnadd.Enabled = false;
                btn_Editor.Enabled = false;
            }
            else if(CheckPopedom() == 2)
            {
                if (lbl_level.Text == "03")
                {
                    btn_del.Enabled = false;
                    //btnadd.Enabled = false;
                    btn_Editor.Enabled = false;
                }
                else
                {
                    string thisRegion = "," + lbl_region.Text + ",";
                    if (MyReginIDList.IndexOf(thisRegion) < 0)
                    {
                        btn_del.Enabled = false;
                        //btnadd.Enabled = false;
                        btn_Editor.Enabled = false;
                    }
                }
            }


        }
    }

    protected void rptSchoolGroup_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName.ToLower() == "btnadd")
        {
            string url = "AddSchool.aspx?Action=add&SID=" + e.CommandArgument.ToString();
            Script.RedirectURL(url);
        }
        else if (e.CommandName.ToLower() == "btnedit")
        {
            string url = "AddSchool.aspx?Action=edit&SID=" + e.CommandArgument.ToString();
            Script.RedirectURL(url);
        }
        else if (e.CommandName.ToLower() == "btndel")
        {
            int count = opSG.Count("SchoolGroupParentId=" + e.CommandArgument.ToString());
            if (count > 1)
            {
                //Label lblmsg = (Label)e.Item.FindControl("lbl_msg_description");
                lbl_msg_description.Text = GetLanageValueByKey("department_msg_g1", PowerCreator.LMS.Language.LanagetItemType.Page);
                //lbl_msg_description.Text = "部门下有子部门，不允许删除该部门!";
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
                return;
            }
            string search = "";
            VOTOSchoolGroup voSchoolGroup = opSG.Load(int.Parse(e.CommandArgument.ToString()));
            //if (voDepartment.DepartmentType == 1)
            //{
            //    search = string.Format("FKTeacherDepartmentId = {0}", e.CommandArgument);
            //    if (opUser.QueryTeacherCount(search) != 0)
            //    {
            //        lbl_msg_description.Text = GetLanageValueByKey("department_msg_g2", PowerCreator.LMS.Language.LanagetItemType.Page);
            //        //lbl_msg_description.Text = "该部门下有有用户，不允许删除该部门!";
            //        Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
            //        return;
            //    }
            //}
            //else if (voDepartment.DepartmentType == 2)
            //{
            //    search = string.Format("FKStudentDepartmentId = {0}", e.CommandArgument);
            //    if (opUser.QueryStudentCount(search) != 0)
            //    {
            //        lbl_msg_description.Text = GetLanageValueByKey("department_msg_g3", PowerCreator.LMS.Language.LanagetItemType.Page);
            //        //lbl_msg_description.Text = "该班级下有有用户，不允许删除该班级!";
            //        Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
            //        return;
            //    }
            //}

            if (opSG.Del(int.Parse(e.CommandArgument.ToString())))
            {
                BinderRpt();
            }
        }
    }

    //protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
    //{
    //    if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
    //    {
    //        LinkButton btn_Editor = (LinkButton)e.Item.FindControl("btn_Editor");
    //        LinkButton btn_del = (LinkButton)e.Item.FindControl("btn_del");
    //        if (CheckUser())
    //        {
    //            lnkbtn_editor.Enabled = true;
    //            lbtn.Enabled = true;
    //            btn_del.Attributes.Add("onClick", "javascript:return confirm('确定删除此记录？');");
    //        }
    //        else
    //        {
    //            lnkbtn_editor.Enabled = false;
    //            lbtn.Enabled = false;
    //        }
    //    }
    //}

}
